# Copyright 2018- The Pixie Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0

load("@io_bazel_rules_docker//container:container.bzl", "container_image", "container_layer")
load("@io_bazel_rules_go//go:def.bzl", "go_cross_binary", "go_library")
load("//bazel:pl_build_system.bzl", "pl_all_supported_go_sdk_versions", "pl_go_binary", "pl_go_sdk_version_template_to_label")

package(default_visibility = ["//src/stirling:__subpackages__"])

container_layer(
    name = "certs_layer",
    directory = "/certs",
    files = ["//src/stirling/testing/demo_apps/go_grpc_tls_pl/certs:server_certs"],
)

go_library(
    name = "server_lib",
    srcs = ["server.go"],
    importpath = "px.dev/pixie/src/stirling/testing/demo_apps/go_grpc_tls_pl/server",
    deps = [
        "//src/stirling/testing/demo_apps/go_grpc_tls_pl/server/greetpb:service_pl_go_proto",
        "@com_github_sirupsen_logrus//:logrus",
        "@com_github_spf13_pflag//:pflag",
        "@com_github_spf13_viper//:viper",
        "@org_golang_google_grpc//:grpc",
        "@org_golang_x_net//http2",
        "@org_golang_x_net//http2/h2c",
    ],
)

pl_go_binary(
    name = "server",
    embed = [":server_lib"],
)

[
    go_cross_binary(
        name = pl_go_sdk_version_template_to_label("golang_%s_grpc_tls_server_binary", sdk_version),
        sdk_version = sdk_version,
        tags = ["manual"],
        target = ":server",
    )
    for sdk_version in pl_all_supported_go_sdk_versions
]

[
    container_image(
        name = pl_go_sdk_version_template_to_label("golang_%s_grpc_tls_server", sdk_version),
        base = "//:pl_go_base_image",
        entrypoint = [
            pl_go_sdk_version_template_to_label("./golang_%s_grpc_tls_server_binary", sdk_version),
            "--server_tls_cert=/certs/server.crt",
            "--server_tls_key=/certs/server.key",
            "--tls_ca_cert=/certs/ca.crt",
        ],
        files = [
            pl_go_sdk_version_template_to_label(":golang_%s_grpc_tls_server_binary", sdk_version),
        ],
        layers = [":certs_layer"],
    )
    for sdk_version in pl_all_supported_go_sdk_versions
]
